import { BasicMarkers } from '../../js/BasicMarkers.js';
import { BasicMarker } from '../../js/BasicMarker.js';
import { useUserInfoStore } from '@gis/lib/pinia/userInfo.js';
import { handleCommonRes } from '@gis/lib/utils/resolver.js';
import { getGisEntityList, getPartyCoordinate, getVoluntaryLocation, getDwcsCoordinateData } from '@gis/lib/api';
import { getMarkerIcon } from '../../js/icon.js';
import openWuZiDialog from '../../../../views/command-dispatching/components/wu_zi_chu_bei/index.js';

const typeCodeEnum = { dbxqwzccd: 'dbxqwzccd', dbxqzz: 'dbxqzz', dbxqdwcs: 'dbxqdwcs' };

// 东部新区 - 指挥调度里面 - 资源
class Marker extends BasicMarker {
  constructor({ map, data }) {
    super({ map, data });
    this.type = typeCodeEnum[data.pointType];
    super.create();
  }
  getIcon(data) {
    return getMarkerIcon(this.type);
  }
  onClick() {
    switch (this.data.pointType) {
      case 'dbxqwzccd': {
        openWuZiDialog(this.data);
        break;
      }
      case 'dbxqzz': {
        break;
      }
      case 'dbxqdwcs': {
        break;
      }
    }
  }
}

export default class Dbxqzy extends BasicMarkers {
  constructor({ map }) {
    super();
    this.map = map;
  }
  createMarker(data) {
    return new Marker({ map: this.map, data });
  }
  async getDataAsync() {
    // 请求 - 物资储备点、组织、场所
    const { area_code } = useUserInfoStore();
    let wzData = await handleCommonRes(
      getGisEntityList,
      {
        area_code: area_code,
        type_code: "'2_07'",
      },
      { defaultValue: [] }
    );
    wzData = wzData.map((item) => Object.assign({}, item, { pointType: 'dbxqwzccd' }));

    let shzzData = await handleCommonRes(
      getPartyCoordinate,
      { zz_type: 'D0060_7', area_code: area_code },
      { defaultValue: [] }
    );
    let zzzzData = await handleCommonRes(getVoluntaryLocation, { area_code: area_code }, { defaultValue: [] });
    let zzData = []
      .concat(shzzData)
      .concat(zzzzData)
      .map((item) => Object.assign({}, item, { pointType: 'dbxqzz' }));

    let csData = await handleCommonRes(
      getDwcsCoordinateData,
      { type: '6', area_code: area_code },
      // { type: '4', dType: 'dwcs', area_code: area_code },
      { defaultValue: [] }
    );
    csData = csData.map((item) => Object.assign({}, item, { pointType: 'dbxqdwcs' }));
    let data = []
      .concat(wzData)
      .concat(zzData)
      .concat(csData)
      .filter((item) => item.longitude && item.latitude)
      .map((item) => Object.assign({}, item, { lnglat: [item.longitude, item.latitude] }));
    return data;
  }
}
